package com.hakim.wfb.customer.service.task;

import com.hakim.wfb.bean.BenefitRecord;
import com.hakim.wfb.bean.ConsumeRecord;
import com.hakim.wfb.biz.BenefitRecordBiz;
import com.hakim.wfb.customer.service.biz.StaticProfitBizService;
import com.hakim.wfb.dao.ConsumeRecordDao;
import com.hkfs.fundamental.common.utils.NumberUtils;
import com.hkfs.fundamental.lock.redis.LockService;
import com.hkfs.fundamental.spring.utils.SpringContextUtils;
import com.hakim.wfb.common.util.LockerUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.TimerTask;

/**
 * Created by Administrator on 2017/7/23.
 */
public class StaticProfitTask24 extends TimerTask {

    private static Logger logger = LoggerFactory.getLogger(StaticProfitTask.class);


    private ConsumeRecordDao consumeRecordDao;
    private StaticProfitBizService staticProfitBizService;

    private BenefitRecordBiz benefitRecordBiz;

    private void delay() {
        try {
            Thread.sleep(1000 * 120);
        } catch (Exception e) {
        }
    }

    public static  void main(String[] args){
        StaticProfitTask24 mya=new StaticProfitTask24();
        Thread testA=new Thread(mya);
        testA.start();
    }

    @Override
    public void run() {

        while (true) {
            try {
                if (SpringContextUtils.getSpringContext() != null)
                    break;

            } catch (Exception e) {
                delay();
                continue;
            }
        }
        consumeRecordDao = (ConsumeRecordDao) SpringContextUtils.getBean("consumeRecordDao");
        List<ConsumeRecord> list = consumeRecordDao.getAllRunningConsumeRecords();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date date0 = calendar.getTime();
        //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //System.out.println("当前时间：" + format.format(date0));

        for (ConsumeRecord consumeRecord : list) {

            //System.out.println("投资时间：" + format.format(consumeRecord.getCrInvestTime()));
            if (consumeRecord.getCrInvestTime().after(date0))
                continue;
            benefitRecordBiz = (BenefitRecordBiz) SpringContextUtils.getBean("benefitRecordBiz");
            BenefitRecord benefitRecord = benefitRecordBiz.getBenefitRecord(consumeRecord.getTid());
            if (benefitRecord != null)
                return;

            System.out.println("计算 uuid：" + consumeRecord.getCrUser() + "的静态收益。");
            logger.info("计算 uuid：" + consumeRecord.getCrUser() + "的静态收益。");
            staticProfitBizService = (StaticProfitBizService) SpringContextUtils.getBean("staticProfitBiz");

            Integer lockId = NumberUtils.random(0, 1000000);
            LockService lockService = (LockService) SpringContextUtils.getBean("lockService");
            Boolean isLockSuccess = false;
            try {
                isLockSuccess = lockService.lock(LockerUtils.UNIQUE_LOCK_Key, LockService.DEFAULT_TRY_LOCK_MILLIS,
                        LockService.DEFAULT_AUTO_UNLOCK_MILLIS, lockId);

                if (!isLockSuccess) {
                    continue;
                }
                staticProfitBizService.run(consumeRecord);
            } catch (Exception e) {

            } finally {
                if (isLockSuccess) {
                    if (lockService != null)
                        lockService.unlock(LockerUtils.UNIQUE_LOCK_Key, lockId);
                }
            }

            System.out.println("计算完毕 uuid：" + consumeRecord.getCrUser() + "的静态收益。");
            logger.info("计算完毕 uuid：" + consumeRecord.getCrUser() + "的静态收益。");
        }

    }
}